package com.wyj.wuliwuli.service.impl;

import com.wyj.wuliwuli.common.AjaxRequest;
import com.wyj.wuliwuli.common.AjaxResult;
import com.wyj.wuliwuli.common.PageParam;
import com.wyj.wuliwuli.entity.WuliUserVideoLike;
import com.wyj.wuliwuli.entity.WuliVideo;
import com.wyj.wuliwuli.entity.WuliVideoComment;
import com.wyj.wuliwuli.entity.WuliVideoCommentDetail;
import com.wyj.wuliwuli.entity.mapper.WuliVideoCommentDetailMapper;
import com.wyj.wuliwuli.entity.mapper.WuliVideoCommentMapper;
import com.wyj.wuliwuli.service.WuliVideoCommentDetailService;
import com.wyj.wuliwuli.service.WuliVideoService;
import com.wyj.wuliwuli.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class WuliVideoCommentDetailServiceImpl implements WuliVideoCommentDetailService {
    private final WuliVideoCommentMapper wuliVideoCommentMapper;
    private final WuliVideoCommentDetailMapper wuliVideoCommentDetailMapper;

    /**
     * 分页查询
     *
     * @param ajaxRequest
     * @return
     */
    @Override
    public AjaxResult page(AjaxRequest<PageParam> ajaxRequest) {
        PageParam pageParam = ajaxRequest.getData();
        Pageable pageable = PageRequest.of(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getDirection(), pageParam.getProperty());
        Page<WuliVideoCommentDetail> page = wuliVideoCommentDetailMapper.findAll(pageable);
        return AjaxResult.ok(page);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public AjaxResult getById(Long id) {
        return null;
    }

    /**
     * 保存
     *
     * @param ajaxRequest
     * @return
     */
    @Override
    public AjaxResult save(AjaxRequest<WuliVideoCommentDetail> ajaxRequest) {
        WuliVideoCommentDetail wuliVideoCommentDetail = ajaxRequest.getData();
        String checkmsg = this.saveCheck(wuliVideoCommentDetail);
        if (!StringUtils.isBlank(checkmsg)) {
            return AjaxResult.fail(checkmsg);
        }
        wuliVideoCommentDetailMapper.save(wuliVideoCommentDetail);
        return AjaxResult.of();
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @Override
    public AjaxResult deleteById(Long id) {

        if (id == null) {
            return AjaxResult.fail("id不能为空");
        }
        if (wuliVideoCommentDetailMapper.existsById(id)) {
            wuliVideoCommentDetailMapper.deleteById(id);
        } else {
            return AjaxResult.fail("id不存在或已被删除");
        }
        return AjaxResult.of();
    }

    public String saveCheck(WuliVideoCommentDetail wuliVideoCommentDetail) {
        if (wuliVideoCommentDetail == null) {
            return "评论不存在";
        }
        if (wuliVideoCommentDetail.getComment() == null) {
            return "评论不能为空";
        }
        if (wuliVideoCommentDetail.getFromUser() == null) {
            return "评论的发布者不能为空";
        }
        if (wuliVideoCommentDetail.getToUser() == null) {
            return "评论的对象不能为空";
        }
        if (StringUtils.isBlank(wuliVideoCommentDetail.getMsg())) {
            return "评论的信息内容不能为空";
        }
        return null;
    }

    /**
     * 根据评论ID查询所有明细
     *
     * @param commentId
     * @return
     */
    @Override
    public AjaxResult details(Long commentId) {
        if (commentId == null) {
            return AjaxResult.fail("评论id不能为空");
        }
        Optional<WuliVideoComment> wuliVideoComment = wuliVideoCommentMapper.findById(commentId);
        if (!wuliVideoComment.isPresent()) {
            return AjaxResult.fail("该id的评论已被删除");
        }
//        WuliVideoCommentDetail detail = new WuliVideoCommentDetail();
//        detail.setComment(wuliVideoComment.get());
//        Example<WuliVideoCommentDetail> example = Example.of(detail);
//        List<WuliVideoCommentDetail> details = wuliVideoCommentDetailMapper.findAll(example, Sort.by(Sort.Order.asc("createDateTime")));
        List<WuliVideoCommentDetail> details = wuliVideoCommentDetailMapper.findByComment_Id(commentId);
        return AjaxResult.ok(details);

    }
}

